package me.zhengjie.modules.easycode.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import me.zhengjie.constant.DefaultConfig;
import me.zhengjie.modules.easycode.domain.Project;
import me.zhengjie.modules.easycode.domain.ProjectTmg;
import me.zhengjie.modules.easycode.domain.*;
import me.zhengjie.modules.easycode.service.ProjectService;
import me.zhengjie.modules.easycode.service.*;
import me.zhengjie.modules.easycode.service.dto.TypeMapperGroupAddParam;
import me.zhengjie.modules.easycode.service.dto.TypeMapperGroupDeleteParam;
import me.zhengjie.modules.easycode.service.mapper.ProjectMapper;
import me.zhengjie.modules.easycode.service.mapper.ProjectTmgMapper;
import me.zhengjie.modules.easycode.service.mapper.TypeMapperGroupMapper;
import me.zhengjie.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author guoqing
 * @date 2021-06-17
 */
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TypeMapperGroupServiceImpl implements TypeMapperGroupService {

    private final TypeMapperGroupMapper typeMapperGroupMapper;
    private final ProjectService projectService;
    private final ProjectMapper projectMapper;
    private final ProjectTmgMapper projectTmgMapper;

    @Override
    @Transactional
    public Result create(TypeMapperGroupAddParam param) {
        //校验参数
        Result verification = verificationNull(param);
        if (verification != null) return verification;
        final Project project = projectService.getByProjectCode(param.getProjectCode());
        if (ObjectUtil.isEmpty(project)) {
            return Result.error("项目不存在");
        }
        final TypeMapperGroup group = new TypeMapperGroup();
        group.setGroupName(param.getGroupName());
        final int insert = typeMapperGroupMapper.insert(group);

        if (insert > 0) {
            project.setCurrentTmGroupId(group.getId());
            projectMapper.updateById(project);
            projectTmgMapper.insert(new ProjectTmg(project.getProjectId(), group.getId()));
        } else {
            Result.error("创建失败");
        }
        return Result.success();
    }

    private Result verificationNull(TypeMapperGroupAddParam param) {
        if (ObjectUtil.isEmpty(param)) {
            return Result.error("参数错误，添加失败");
        }
        if (StrUtil.isEmpty(param.getProjectCode())) {
            return Result.error("项目编号不存在");
        }
        if (StrUtil.isEmpty(param.getGroupName())) {
            return Result.error("分组名称不能为空");
        }
        return null;
    }

    @Override
    @Transactional
    public Result removeById(TypeMapperGroupDeleteParam param) {
        if (ObjectUtil.isEmpty(param) || param.getTypeMapperGroupid() == null || param.getTypeMapperGroupid().equals(0)) {
            return Result.error("组id缺失");
        }
        typeMapperGroupMapper.deleteById(param.getTypeMapperGroupid());
        final TypeMapperGroup group = typeMapperGroupMapper.selectOne(new QueryWrapper<TypeMapperGroup>().eq("group_name", DefaultConfig.DEFAULT));
        // 更新项目当前映射器组
        final Project project = projectService.getByProjectCode(param.getProjectCode());
        project.setCurrentTmGroupId(group.getId());
        projectMapper.updateById(project);
        projectTmgMapper.delete(new QueryWrapper<ProjectTmg>().eq("project_id", project.getProjectId()).eq("type_mapper_group_id", param.getTypeMapperGroupid()));
        return Result.success();
    }
}
